<?php

namespace app\admin\controller;

use app\comm\func\CommFunc;
use app\comm\func\Consts;
use think\Controller;
use think\Request;
use think\Db;

/**
 * 系统参数设置页面
 * Class System
 * @package app\admin\controller
 */
class System extends Controller
{
    /**
     * 系统列表
     * @param Request $request
     */
    public function SystemList(Request $request)
    {
        $system = Db::table('system')->order('type')->select();
        $this->assign('system', $system);
        return $this->fetch();
    }

    /**
     * 系统选项增加
     * @param Request $request
     */
    public function SystemAdd(Request $request)
    {
        if ($request->isPost()) {
            CommFunc::emptyVerify($request->post(), ['option', 'type']);
            CommFunc::conditionAdd($condition, $request->post(), ['option', 'type']);
            $selectType = Db::table('system')->where('option', $condition['option'])->find();
            if ($selectType) {
                CommFunc::ajaxReturn(Consts::$failure);
            }
            $insertType = Db::table('system')->insert($condition);
            if ($insertType > 0) {
                CommFunc::ajaxReturn(Consts::$successful);
            } else {
                CommFunc::ajaxReturn(Consts::$failure);
            }
        } else {
            return $this->fetch();
        }
    }

    /**
     * 系统选项修改
     * @param Request $request
     */
    public function SystemEdit(Request $request)
    {
        if ($request->isPost()) {
            CommFunc::emptyVerify($request->post(), ['option', 'type', 'id']);
            CommFunc::conditionAdd($condition, $request->post(), ['option', 'type', 'id']);
            if (in_array($condition['id'], [1, 2, 3, 4, 5, 6, 7])) {
                CommFunc::ajaxReturn(Consts::$failure);
            }
            $updateType = Db::table('system')
                ->where('id', $condition['id'])
                ->data([
                    'option' => $condition['option'],
                    'type' => $condition['type']
                ])->update();
            if ($updateType > 0) {
                CommFunc::ajaxReturn(Consts::$successful);
            } else {
                CommFunc::ajaxReturn(Consts::$failure);
            }
        } else {
            CommFunc::emptyVerify($request->param(), ['id']);
            CommFunc::conditionAdd($condition, $request->param(), ['id']);
            $system = Db::table('system')->where('id', $condition['id'])->find();
            $this->assign('system', $system);
            return $this->fetch();
        }
    }

    /**
     * 系统选项删除
     * @param Request $request
     */
    public function SystemDel(Request $request)
    {
        CommFunc::emptyVerify($request->post(), ['id']);
        CommFunc::conditionAdd($condition, $request->post(), ['id']);
        if (in_array($condition['id'], [1, 2, 3, 4, 5, 6, 7])) {
            CommFunc::ajaxReturn(Consts::$failure);
        }
        $deleteType = Db::table('system')
            ->where('id', $condition['id'])
            ->delete();
        if ($deleteType > 0) {
            CommFunc::ajaxReturn(Consts::$successful);
        } else {
            CommFunc::ajaxReturn(Consts::$failure);
        }
    }
}